package com.dongdongshop.controller;

import com.dongdongshop.data.PageBean;
import com.dongdongshop.data.Result;
import com.dongdongshop.data.ResultEnum;
import com.dongdongshop.pojo.TbBrand;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.TbTypeTemplate;
import com.dongdongshop.service.TemplateService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("tempalte")
public class TemplateController {

    @DubboReference
    private TemplateService templateService;

    @RequestMapping("toTemplate")
    public String tempalteList(Model model) {
        List<TbBrand> brandList = templateService.getBrandList();
        model.addAttribute("brandList",brandList);
        List<TbSpecification> specList = templateService.getSpecList();
        model.addAttribute("specList",specList);
        return "admin/type_template";
    }

    @RequestMapping("getTemplateInfo")
    @ResponseBody
    public PageBean<TbTypeTemplate> getTemplateInfo(@RequestParam(defaultValue = "1")Integer pageNumber,@RequestParam(defaultValue = "5")Integer pageSize,String name) {
        try {
            PageBean<TbTypeTemplate> pb = templateService.getTemplateInfo(pageNumber,pageSize,name);
            return pb;
        }catch (Exception e) {
            return null;
        }
    }

    @RequestMapping("addTemplate")
    @ResponseBody
    public Result addTemplate(TbTypeTemplate template) {
        try {
            templateService.addTemplate(template);
            return Result.respones(ResultEnum.SUCCESS);
        }catch (Exception e) {
            return Result.respones(ResultEnum.ERROR);
        }
    }

    @RequestMapping("deleteAll")
    @ResponseBody
    public Result deleteAll(Long[] ids) {
        try {
            templateService.deleteAll(ids);
            return Result.respones(ResultEnum.SUCCESS);
        }catch (Exception e) {
            return Result.respones(ResultEnum.ERROR);
        }
    }

    @RequestMapping("getOneTemplate")
    @ResponseBody
    public Result getOneTemplate(Long id) {
        try {
            TbTypeTemplate template = templateService.getOneTemplate(id);
            return Result.ok().setData(template);
        }catch (Exception e) {
            return Result.respones(ResultEnum.ERROR);
        }
    }

    @RequestMapping("deleteTemplate")
    @ResponseBody
    public Result deleteTemplate(Long id) {
        try {
            templateService.deleteTemplate(id);
            return Result.respones(ResultEnum.SUCCESS);
        }catch (Exception e) {
            return Result.respones(ResultEnum.ERROR);
        }
    }

    @RequestMapping("updateTemplate")
    @ResponseBody
    public Result updateTemplate(TbTypeTemplate template) {
        try {
            templateService.updateTemplate(template);
            return Result.respones(ResultEnum.SUCCESS);
        }catch (Exception e) {
            return Result.respones(ResultEnum.ERROR);
        }
    }



}
